<%@ page language="java"%>
<%@ page import="java.util.*"%>
<%@ page import="org.cgs.web.beans.*"%>

<jsp:useBean id="instructorBean" scope="session" class="org.cgs.web.beans.InstructorHelperBean" />
<jsp:setProperty name="instructorBean" property="*" />

<%
if(instructorBean.hasAllInput())
{
	List<String> courses = instructorBean.authenticateInstructor();
	if(courses.size() <= 0)
	{
%>
	<script type="text/javaScript">
		alert("The username/password combination used when attempting to login was incorrect");
	</script>
<%
	}
	else if(courses.size() == 1)
	{
		instructorBean.setCourseCode(courses.get(0));
		instructorBean.getInstructorRole();
	
		request.getSession().setAttribute("loggedIn", "true");
		request.getSession().setAttribute("courseCode", instructorBean.getCourseCode());
		response.sendRedirect("instructor/instructorMain.jsp");
	}
	else
	{
		response.sendRedirect("instructor/myCourses.jsp");
	}
}
%>

<html>
	<head>
		<title>Instructor Login</title>
		<script type="text/javascript">
		    function validateInput()			{				if(f.username.value == "")				{					alert("Please enter a valid username and try again");					return false;				} //end if				else if (f.password.value == "")				{					alert("Please enter a valid password and try again");					return false;				} //end if			}
		</script>
	</head>

	<body>
		<center>
			<form id="f" method="post" action="instructorLogin.jsp" onsubmit="return validateInput()">
				<table>
					<tr>
						<td>Username:</td>
						<td><input type="text" name="username"></td>
					</tr>
					<tr>
						<td>Password:</td>
						<td><input type="password" name="password"></td>
					</tr>
					<tr>
						<td colspan="2" align="center"><input type="submit" value="Login"></td>
					</tr>
				</table>
			</form>
		</center>
	</body>
</html>